package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.basic.wcs.utils.Coord;
import com.suray.commin.core.utils.StringUtils;
import com.suray.commin.enmus.DeviceStatus;
import com.suray.wcs.service.enums.PortModel;
import com.suray.wcs.service.system.entity.ProgrammeDB;
import com.suray.wcs.service.system.mapper.ProgrammeMapper;
import com.suray.wcs.service.system.service.IProgrammeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:库口配置服务实现
 * @author: rongbj
 * @date:2019年12月5日 下午7:52:57
 */
@Service
public class ProgrammeServiceImpl extends ServiceImpl<ProgrammeMapper, ProgrammeDB> implements IProgrammeService {
    /**
     * @param portType
     * @return
     */
    @Override
    public List<ProgrammeDB> getProgrammes(Integer portType) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<ProgrammeDB>();
        queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, portType);
        return list(queryWrapper);
    }

    @Override
    public List<ProgrammeDB> queryProgrammeConf(ProgrammeDB programmeDB) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<ProgrammeDB>();
        if (StringUtils.isNotEmpty(programmeDB.getProgrammeName())) {
            queryWrapper.lambda().like(ProgrammeDB::getProgrammeName, programmeDB.getProgrammeName());
        }
        if (programmeDB.getProgrammeType() != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, programmeDB.getProgrammeType());
        }
        if (programmeDB.getStatus() != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getStatus, programmeDB.getStatus());
        }
        if (programmeDB.getPortId() != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getPortId, programmeDB.getPortId());
        }
        if (programmeDB.getProgrammeType() != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, programmeDB.getProgrammeType());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void insertProgrammeDB(ProgrammeDB programmeDB) {
        this.save(programmeDB);
    }

    @Override
    public void updateProgrammeDB(ProgrammeDB programmeDB) {
        this.updateById(programmeDB);
    }

    @Override
    public ProgrammeDB queryProgrammeDB(Integer portType, Integer programmeId) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, portType).eq(ProgrammeDB::getProgrammeId, programmeId);
        return getOne(queryWrapper);
    }

    @Override
    public int updateProgrammeStatus(ProgrammeDB programmeDB) {
        return baseMapper.updateById(programmeDB);
    }

    /**
     * 通过方案ID获取
     *
     * @param programmeId
     * @return
     */
    @Override
    public ProgrammeDB selectByProgrammeId(Long programmeId) {
        return baseMapper.selectById(programmeId);
    }

    @Override
    public List<ProgrammeDB> findByPortId(Integer portId) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProgrammeDB::getPortId, portId);
        queryWrapper.lambda().eq(ProgrammeDB::getStatus, DeviceStatus.OK.getCode());
        return list(queryWrapper);
    }

    /**
     * 根据WMS发送的任务查找所有可执行方案
     *
     * @param portId   库口
     * @param portType 库口类型
     * @return
     */
    public List<ProgrammeDB> findByDistrictAndPortId(Integer portId, PortModel portType) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<>();
        if (portId != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getPortId, portId);
        }
        if (portType != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, portType.getVal());
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据库口方案坐标查询库口方案
     *
     * @param coord
     * @param portType
     * @return
     */
    public List<ProgrammeDB> findByCoords(Integer taskPortId, Coord coord, PortModel portType) {
        QueryWrapper<ProgrammeDB> queryWrapper = new QueryWrapper<>();
        if (taskPortId != null) {
            queryWrapper.lambda().like(ProgrammeDB::getPortId, taskPortId);
        }
        if (coord != null) {
            queryWrapper.lambda().like(ProgrammeDB::getCoords, coord.toString());
        }
        if (portType != null) {
            queryWrapper.lambda().eq(ProgrammeDB::getProgrammeType, portType.getVal().intValue());
        }
        List<ProgrammeDB> programmeDBS = this.list(queryWrapper);
        return programmeDBS;
    }
}
